/**
 * $RCSfile$
 * $Revision$
 * $Date$
 * $Source$
 * $Author$
 */
package pl.edu.agh.loganalizator.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import pl.edu.agh.loganalizator.model.Rule;
import pl.edu.agh.loganalizator.model.RuleModel;
import pl.edu.agh.loganalizator.presenter.Parser;

public class View extends JFrame implements RuleInterface {
  private static JTextArea editArea;
  private static JFileChooser fileChooser = new JFileChooser();
  private static RulePanelView ruleView = new RulePanelView();

  //operacje na pliku
  private static Action openAction;
  private static Action saveAction;
  private static Action parseAction;
  private static Action exitAction;

  // operacje na regule
  private static Action addRuleAction;

  private Parser parse;
  static View view;
  RuleModel ruleModel;

  public View() {
    addRuleAction = new AddRuleAction();
    openAction = new OpenAction();
    saveAction = new SaveAction();
    parseAction = new ParseAction();
    exitAction = new ExitAction();
    fileChooser = new JFileChooser();
    ruleView = new RulePanelView();

    ruleModel = new RuleModel();
    editArea = new JTextArea(15, 80);
    editArea.setEditable(false);
    editArea.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    editArea.setFont(new Font("monospaced", Font.PLAIN, 14));
    JScrollPane scrollingText = new JScrollPane(editArea);

    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());

    content.add(scrollingText, BorderLayout.CENTER);
    content.add(ruleView, BorderLayout.EAST);

    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = menuBar.add(new JMenu("Plik"));
    fileMenu.setMnemonic('P');
    fileMenu.add(openAction);
    fileMenu.add(saveAction);
    fileMenu.addSeparator();
    fileMenu.add(parseAction);
    fileMenu.addSeparator();
    fileMenu.add(exitAction);

    JMenu ruleMenu = menuBar.add(new JMenu("Reguła"));
    ruleMenu.setMnemonic('R');
    ruleMenu.add(addRuleAction);

    setContentPane(content);
    setJMenuBar(menuBar);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Systemy Rozproszone - Analizator logów");
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }

  public static void main(String[] args) {
    view = new View();
  }

  public class AddRuleAction extends AbstractAction {
    AddRuleAction() {
      super("Dodaj regułę");
      putValue(MNEMONIC_KEY, new Integer('D'));
    }

    public void actionPerformed(ActionEvent e) {
      if (parse != null) {
        parse.setRuleModel(ruleModel);
        AddRuleDialogFrame addRuleDialogFrame = new AddRuleDialogFrame(ruleModel, parse.getColumnModel(), view);
        addRuleDialogFrame.setLocationRelativeTo(view);
      }
    }
  }

  public class ParseAction extends AbstractAction {
    ParseAction() {
      super("Parsuj...");
      putValue(MNEMONIC_KEY, new Integer('P'));
    }

    public void actionPerformed(ActionEvent e) {
      if (parse != null) {
        parse.parse();
        System.out.println(ruleModel.getRuleArrayList());
        editArea.setText(parse.getParsedText());
      }
    }
  }

  // akcje na pliku - STAR
  public class OpenAction extends AbstractAction {
    public OpenAction() {
      super("Otwórz...");
      putValue(MNEMONIC_KEY, new Integer('O'));
    }

    public void actionPerformed(ActionEvent e) {
      int retval = fileChooser.showOpenDialog(View.this);
      if (retval == JFileChooser.APPROVE_OPTION) {
        File f = fileChooser.getSelectedFile();
        try {
          parse = new Parser(f);
          editArea.setText(parse.getParsedText());
        }
        catch (IOException ioex) {
          System.out.println(e);
          System.exit(1);
        }
      }
    }
  }

  public class SaveAction extends AbstractAction {
    SaveAction() {
      super("Zapisz...");
      putValue(MNEMONIC_KEY, new Integer('S'));
    }

    public void actionPerformed(ActionEvent e) {
      int retval = fileChooser.showSaveDialog(View.this);
      if (retval == JFileChooser.APPROVE_OPTION) {
        File f = fileChooser.getSelectedFile();
        try {
          FileWriter writer = new FileWriter(f);
          editArea.write(writer);
        }
        catch (IOException ioex) {
          JOptionPane.showMessageDialog(View.this, ioex);
          System.exit(1);
        }
      }
    }
  }

  public class ExitAction extends AbstractAction {
    public ExitAction() {
      super("Wyjście");
      putValue(MNEMONIC_KEY, new Integer('X'));
    }

    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  }

  @Override
  public void addRule(Rule rule) {
    ruleModel.addRuleAction(rule);
    ruleView.add(new RuleView(rule));
    ruleView.updateUI();
    ruleView.repaint();
    view.repaint();
  }

  @Override
  public void editRule(Rule rule) {
  // TODO Auto-generated method stub

  }

  @Override
  public void removeRule(Rule rule) {
  // TODO Auto-generated method stub

  }

  // akcje na pliku - STOP
}
